<?php
namespace Article\Model;
use Think\Model;
class ArticleModel extends Model {

	public $page; //当前页
	public $perpage; //每页显示
	public $category_id; //分类ID
	public $article_id; //文章ID

	//判断是否管理员
	/*public function check_admin($arr)
	{
		$User = M("User"); // 实例化User对象
		$condition['username'] = $arr['username'];
		$condition['password'] = $arr['password'];
		$condition['is_admin'] = 2; //2为管理员
		$condition['_logic'] = 'and';
		// 把查询条件传入查询方法
		$count=$User->where($condition)->count();
		if($count==0){
			return false;
		}else{
			return true;
		}

	}*/

	//获得分类列表
	/*public function get_category_list()
	{
		$Category = M("Category"); // 实例化对象
		$field[]='category_id';
		$field[]='category_name';
		$where['parent_category_id'] = array('gt',0);
		$order['sort']='asc';
		$order['category_id']='asc';
		// 把查询条件传入查询方法
		$rows=$Category->field($field)->where($where)->order($order)->select();
		return $rows;
	}*/

	//提交文章
	public function add_article_post($arr)
	{
		$Model = M("Article"); // 实例化对象
		$arr['article_add_time'] = time(); //当前时间
		// 写入
		if($Model->add($arr)){
			return true;
		}else{
			return false;
		}
	}

	//获得文章列表 $page当前页 $perpage每页显示
	public function get_article_list($arr)
	{
		//$category_id=$arr['category_id'];
		$page=$arr['page'];
		$perpage=$arr['perpage'];
		$start=($page-1)*$perpage;
		$Model=M('Article');
		$rows=$Model->query("select a.article_id,a.article_title,a.article_thumb,a.article_type,a.article_add_time,a.article_show,b.category_id,b.category_name from `think_article` a left join `think_article_category` b on a.category_id=b.category_id order by b.category_sort asc,a.article_type asc,a.article_sort asc,a.article_id asc limit $start,$perpage");
		//dump($rows);exit;
		return $rows;

	}


	//获得文章总数
	public function get_article_total($arr)
	{
		$Model=M("Article");
		if($arr['category_id']!=''){
			$condition['category_id'] = $arr['category_id'];
		}
		$condition['_logic'] = 'and';
		// 把查询条件传入查询方法
		$count=$Model->where($condition)->count();
		//echo $count;exit;
		return $count;
	}

	//获得分类列表
	public function get_article_info($article_id)
	{
		$article_id=intval($article_id);
		$Model=M("Article"); // 实例化对象
		//$field[]='article_title';
		//$field[]='article_content';
		//$field[]='category_id';
		$condition['article_id']=$article_id;
		$condition['_logic'] = 'and';
		// 把查询条件传入查询方法
		//$info=$Model->field($field)->where($condition)->find();
		$info=$Model->where($condition)->find();
		return $info;
	}

	//编辑文章 写入数据表
	public function edit_article_post($arr)
	{
		$Model=M("Article"); // 实例化对象
		$arr['article_edit_time'] = time(); //当前时间
		$condition['article_id'] = $arr['article_id'];
		// 写入
		if($Model->where($condition)->save($arr)){
			return true;
		}else{
			return false;
		}
	}

	//删除文章
	public function delete_article()
	{
		$article_id=intval($this->article_id);
		$Model=M("Article"); // 实例化对象
		$condition['article_id']=$article_id;
		if($Model->where($condition)->delete()){
			return true;
		}else{
			return false;
		}
	}

	//提交目录
	public function add_category_post($arr)
	{
		$Model=M("ArticleCategory"); // 实例化对象
		// 写入
		if($Model->add($arr)){
			return true;
		}else{
			return false;
		}
	}

	//取得所有目录列表
	public function get_all_category_list()
	{
		$Model=M("ArticleCategory"); // 实例化对象
		$field[]='category_id';
		$field[]='category_name';
		$field[]='category_parent_id';
		$field[]='category_sort';
		$condition['category_show'] = 1; //1显示
		$condition['_logic'] = 'and';
		$order['category_sort']='asc';
		$order['category_id']='asc';
		//查询获得大致的表
		$rows=$Model->field($field)->where($condition)->order($order)->select();
		//整理表 通过parent_category查询category_name
		foreach($rows as $k=>$v){
			if($v['category_parent_id']==0){
				$rows[$k]['category_parent_name']='顶级目录';
			}else{
				$rows[$k]['category_parent_name']=$Model->where('category_id='.$v['category_parent_id'])->getField('category_name');
			}

		}
		return $rows;
	}

	//获得分类列表
	public function get_parent_category_list()
	{
		$Model= M("ArticleCategory"); // 实例化对象
		$field[]='category_id';
		$field[]='category_name';
		$where['category_parent_id'] = array('eq',0);
		$order['category_sort']='asc';
		$order['category_id']='asc';
		// 把查询条件传入查询方法
		$rows=$Model->field($field)->where($where)->order($order)->select();
		return $rows;
	}

	//获得目录总数
	public function get_category_total()
	{
		$Model=M("ArticleCategory");
		$count=$Model->count();
		return $count;
	}

	//获得目录信息
	public function get_category_info()
	{
		$category_id=intval($this->category_id);
		$Model=M("ArticleCategory"); // 实例化对象
		$field[]='category_name';
		$field[]='category_parent_id';
		$field[]='category_sort';
		$condition['category_id']=$category_id;
		// 把查询条件传入查询方法
		$rows=$Model->field($field)->where($condition)->limit(1)->select();
		return $rows[0];
	}

	//编辑目录 写入数据表
	public function edit_category_post($arr)
	{
		$Model=M("ArticleCategory"); // 实例化对象
		$condition['category_id'] = $arr['category_id'];
		// 写入
		if($Model->where($condition)->save($arr)){
			return true;
		}else{
			return false;
		}
	}

}